package com.example.flowable.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.flowable.model.Holiday;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.flowable.engine.IdentityService;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "huiqian")
public class HuiqianController {
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private IdentityService identityService;

    /**
     * 部署会签流程
     */
    @RequestMapping(value = "deploy")
    @ResponseBody
    public String deploy() {
        //部署流程
        Deployment deployment = processEngine.getRepositoryService().createDeployment()
//                .name("请假流程")
                .addClasspathResource("flowable/huiqian.bpmn20.xml")
                .deploy();
        return "部署成功.流程Id为：" + deployment.getId();
    }

    /**
     * 发起会签流程
     */
    @RequestMapping(value = "add")
    @ResponseBody
    public String adHuiqian(@RequestBody Holiday holiday) {
        //启动流程
        String jsonString = JSONObject.toJSONString(holiday);
        Map<String, Object> map = JSON.parseObject(jsonString,Map.class);
        // 设置流程启动用户
        identityService.setAuthenticatedUserId(holiday.getUserName());
        // 设置下一审批组
        List<String> assigneeList = new ArrayList<>();
        assigneeList.add("assignee11");
        assigneeList.add("assignee12");
        assigneeList.add("assignee13");
        map.put("assigneeList", assigneeList);
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(holiday.getProcessDefinitionKey(),"businessKey111", map);
        // TODO 手动设置候选人
        // 查询task
        // List<Task> taskList = taskService.createTaskQuery().processInstanceId(processInstance.getId()).list();
        // taskService.addCandidateUser(taskList.get(0).getId(), "7");
        return "请假审批流程开始.流程Id为：" + processInstance.getId();
    }

    /**
     * 执行管理员任务
     */
    @RequestMapping(value = "/approve")
    @ResponseBody
    public Object approve(boolean approve, String taskId, String processInstanceId, String assignee){
        System.out.println(approve? "批准":"退回");
        HashMap<String, Object> map = new HashMap<>();
        map.put("holidayExeType", approve?"Y":"N");
        // if (!StringUtils.isEmpty(assignee)) {
        //     map.put("assignee", assignee);
        // }
        // map.put("secend", "1");
        taskService.complete(taskId, map);
        // TODO 手动设置候选人
        // 查询task，不能直接用上面的taskId，因为完成之后taskId会变，secend差审批完就结束流程了，数据就到history中了，这里查询不到结果
        // List<Task> taskList = taskService.createTaskQuery().processInstanceId(processInstanceId).list();
        // if (!taskList.isEmpty()) {
        //     taskService.addCandidateUser(taskList.get(0).getId(), "7");
        // }
        return approve? "批准":"退回";
    }


    /**
     * 执行管理员任务
     */
    @RequestMapping(value = "/approveyipianfoujue")
    @ResponseBody
    public Object approveyipianfoujue(boolean approve, String taskId, String processInstanceId, String assignee){
        HashMap<String, Object> map = new HashMap<>();
        if (!approve) {
            map.put("reject", 1);
        }
        taskService.complete(taskId, map);
        // TODO 手动设置候选人
        // 查询task，不能直接用上面的taskId，因为完成之后taskId会变，secend差审批完就结束流程了，数据就到history中了，这里查询不到结果
        // List<Task> taskList = taskService.createTaskQuery().processInstanceId(processInstanceId).list();
        // if (!taskList.isEmpty()) {
        //     taskService.addCandidateUser(taskList.get(0).getId(), "7");
        // }
        return approve? "批准":"退回";
    }
}
